/*
 * Copyright 2011 Romain Gilles
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jvdb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

/**
 * TODO_LIST
 * - TODO: Keep parent reference (observer pattern) to trace non flushed information due to as missing call to close
 */
class StatementWrapper implements Statement {

    private final Statement delegate;
    private final AbstractConnectionWrapper parent;
    private final int statementId;
    private StatementTimer batchTimer = null;
    StatementWrapper(AbstractConnectionWrapper parent, Statement delegate, int statementId) {
        this.delegate = delegate;
        this.parent = parent;
        this.statementId = statementId;
    }

    public int getResultSetHoldability() throws SQLException {
        return delegate.getResultSetHoldability();
    }

    public void close() throws SQLException {
        delegate.close();
    }

    public int getMaxFieldSize() throws SQLException {
        return delegate.getMaxFieldSize();
    }

    public void setMaxFieldSize(int max) throws SQLException {
        delegate.setMaxFieldSize(max);
    }

    public int getMaxRows() throws SQLException {
        return delegate.getMaxRows();
    }

    public void setMaxRows(int max) throws SQLException {
        delegate.setMaxRows(max);
    }

    public void setEscapeProcessing(boolean enable) throws SQLException {
        delegate.setEscapeProcessing(enable);
    }

    public int getQueryTimeout() throws SQLException {
        return delegate.getQueryTimeout();
    }

    public void setQueryTimeout(int seconds) throws SQLException {
        delegate.setQueryTimeout(seconds);
    }

    public void cancel() throws SQLException {
        delegate.cancel();
    }

    public SQLWarning getWarnings() throws SQLException {
        return delegate.getWarnings();
    }

    public void clearWarnings() throws SQLException {
        delegate.clearWarnings();
    }

    public void setCursorName(String name) throws SQLException {
        delegate.setCursorName(name);
    }


    public ResultSet getResultSet() throws SQLException {
        return delegate.getResultSet();
    }

    public int getUpdateCount() throws SQLException {
        return delegate.getUpdateCount();
    }

    public boolean getMoreResults() throws SQLException {
        return delegate.getMoreResults();
    }

    public void setFetchDirection(int direction) throws SQLException {
        delegate.setFetchDirection(direction);
    }

    public int getFetchDirection() throws SQLException {
        return delegate.getFetchDirection();
    }

    public void setFetchSize(int rows) throws SQLException {
        delegate.setFetchSize(rows);
    }

    public int getFetchSize() throws SQLException {
        return delegate.getFetchSize();
    }

    public int getResultSetConcurrency() throws SQLException {
        return delegate.getResultSetConcurrency();
    }

    public int getResultSetType() throws SQLException {
        return delegate.getResultSetType();
    }

    public Connection getConnection() throws SQLException {
        return parent;
    }

    public boolean getMoreResults(int current) throws SQLException {
        return delegate.getMoreResults(current);
    }

    public ResultSet getGeneratedKeys() throws SQLException {
        return delegate.getGeneratedKeys();
    }



    public ResultSet executeQuery(final String sql) throws SQLException {
        StatementTimer timer = newTimer(sql);
        ResultSet result = delegate.executeQuery(sql);
        timer.end();
        return result;

    }

    public int executeUpdate(final String sql) throws SQLException {
        StatementTimer timer = newTimer(sql);
        int result = delegate.executeUpdate(sql);
        timer.end();
        return result;
    }

    public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
        StatementTimer timer = newTimer(sql);
        int result = delegate.executeUpdate(sql, autoGeneratedKeys);
        timer.end();
        return result;
    }

    private StatementTimer newTimer(String sql) {
        return StatementTimer.newStartedTimer(parent.connectionId, statementId, sql);
    }

    public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
        return delegate.executeUpdate(sql, columnIndexes);
    }

    public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
        return delegate.executeUpdate(sql, columnNames);
    }

    public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
        return delegate.execute(sql, autoGeneratedKeys);
    }

    public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
        return delegate.execute(sql, columnIndexes);
    }

    public boolean execute(final String sql, final String[] columnNames) throws SQLException {
        return delegate.execute(sql, columnNames);
    }

    public boolean execute(final String sql) throws SQLException {
        return delegate.execute(sql);
    }
    //~---------------------------------------------------------------Batch query Handling
    public void addBatch(String sql) throws SQLException {
        delegate.addBatch(sql);
        if (batchTimer == null) {
            batchTimer = StatementTimer.newTimer(parent.connectionId, statementId, sql);
        } else {
            batchTimer.addBatch(sql);
        }
    }

    public void clearBatch() throws SQLException {
        delegate.clearBatch();
        batchTimer = null;
    }

    //execute query
    public int[] executeBatch() throws SQLException {
        if (batchTimer != null) {
            batchTimer.start();
        }
        int[] result = delegate.executeBatch();
        if (batchTimer != null) {
            batchTimer.end();
        }
        return result;
    }

    static Statement newStatementWrapper(AbstractConnectionWrapper parent, Statement delegate, int number) {
        return new StatementWrapper(parent, delegate, number);
    }
}
